import store from "@/store/index"

class Router {
  getUrl(to, param) {
    return param
      ? to + "?param=" + encodeURIComponent(JSON.stringify(param))
      : to
  }

  beforeEach(next) {
    if (true /*store.getters.isLogin*/) {
      next()
    } else {
      uni.navigateTo({
        url: "/pages/login/index",
      })
    }
  }

  back() {
    uni.navigateBack()
  }

  push(to, param) {
    this.beforeEach(() => {
      const url = this.getUrl(to, param)
      uni.navigateTo({
        url: url,
      })
    })
  }

  switchTab(to, param) {
    const url = this.getUrl(to, param)
    uni.switchTab({
      url: url,
    })
  }
}

export default new Router()
